package com.gin.kotlin.no_gay_alliance.fragment.main.setting

import android.text.Html
import android.text.method.LinkMovementMethod
import android.util.Log
import android.widget.CompoundButton
import android.widget.RadioGroup
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingFragment
import com.gin.kotlin.no_gay_alliance.base.BuglyUtils
import com.gin.kotlin.no_gay_alliance.databinding.FragmentMainSettingGlobalPropertyBinding
import com.gin.kotlin.no_gay_alliance.enums.NgaTheme
import com.gin.kotlin.no_gay_alliance.manager.AccountManager
import com.gin.kotlin.no_gay_alliance.store.GlobalProperty
import com.gin.nga.enums.NgaDomain

/**
 * * 全局设置
 * *  包含：界面主题，域名选择，bugly开关
 */
class GlobalPropertyFragment : AbstractBindingFragment<FragmentMainSettingGlobalPropertyBinding>(), RadioGroup.OnCheckedChangeListener, CompoundButton.OnCheckedChangeListener {
    private lateinit var globalProperty: GlobalProperty

    /**
     * onCreateView的后续操作 ，如通过  [FragmentActivity.addMenuProvider] 添加菜单
     */
    override fun onCreateView() {
        val b = this.b ?: return
        b.tvTipsGlobalPropertyBugly.text = Html.fromHtml(BuglyUtils.TIPS + BuglyUtils.LINK, Html.FROM_HTML_MODE_COMPACT)
        b.tvTipsGlobalPropertyBugly.movementMethod = LinkMovementMethod.getInstance()

        this.globalProperty = GlobalProperty.getInstance()
        // 根据全局设置初始化组件状态

        // 主题选择
        b.radioGroupTheme.check(globalProperty.theme.buttonId)
        b.radioGroupTheme.setOnCheckedChangeListener(this)
        b.swGlobalPropertyThemeFollow.isChecked = globalProperty.themeFollow
        b.swGlobalPropertyThemeFollow.setOnCheckedChangeListener(this)

        // 域名选择
        b.radioGroupHost.check(
            when (globalProperty.domain) {
                NgaDomain.MASTER -> R.id.radio_button_host_master
                NgaDomain.SLAVE_BBS -> R.id.radio_button_host_slave_bbs
                NgaDomain.SLAVE_178 -> R.id.radio_button_host_slave_178
                NgaDomain.SLAVE_G -> R.id.radio_button_host_slave_g
            }
        )
        b.radioGroupHost.setOnCheckedChangeListener(this)

        // bugly
        b.swGlobalPropertyBugly.isChecked = BuglyUtils.getEnable()
        b.swGlobalPropertyBugly.setOnCheckedChangeListener(this)

        b.swOnClickTopic.isChecked = globalProperty.openHistoryPage
        b.swOnClickTopic.setOnCheckedChangeListener(this)

        b.swOnLongClickTopic.isChecked = globalProperty.showPagerDialog
        b.swOnLongClickTopic.setOnCheckedChangeListener(this)

        b.swAutoQuoteContent.isChecked = globalProperty.autoQuoteContent
        b.swAutoQuoteContent.setOnCheckedChangeListener(this)

    }

    /**
     * radio group 选中项切换时的操作
     * @param group RadioGroup
     * @param checkedId Int
     */
    override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
        val b = this.b ?: return

        // 主题切换
        if (group == b.radioGroupTheme) {
            val theme = NgaTheme.entries.first { it.buttonId == checkedId }
            globalProperty.theme = theme
            Log.i(TAG, "修改主题配色: ${theme.name}")
            toast(getString(R.string.toast_message_theme_changed))
        }

        // 域名切换
        if (group == b.radioGroupHost) {
            val ngaDomain = when (checkedId) {
                R.id.radio_button_host_master -> NgaDomain.MASTER
                R.id.radio_button_host_slave_bbs -> NgaDomain.SLAVE_BBS
                R.id.radio_button_host_slave_178 -> NgaDomain.SLAVE_178
                R.id.radio_button_host_slave_g -> NgaDomain.SLAVE_G
                else -> throw RuntimeException("不匹配的选项")
            }
            globalProperty.domain = ngaDomain
            // 刷新API
            AccountManager.getInstance().onDomainChanged(ngaDomain)
        }


    }

    /**
     * 开关 状态切换时的操作
     * @param buttonView CompoundButton
     * @param isChecked Boolean
     */
    override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
        val b = this.b ?: return

        if (buttonView == b.swGlobalPropertyBugly) {
            BuglyUtils.setEnable(isChecked, requireContext())
            if (!isChecked) {
                toast(getString(R.string.toast_message_bugly_closed))
            }
        }

        if (buttonView == b.swGlobalPropertyThemeFollow) globalProperty.themeFollow = isChecked

        // 行为
        if (buttonView == b.swOnClickTopic) globalProperty.openHistoryPage = isChecked

        if (buttonView == b.swOnLongClickTopic) globalProperty.showPagerDialog = isChecked

        if (buttonView == b.swAutoQuoteContent) globalProperty.autoQuoteContent = isChecked


    }
}